package com.xym.web.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xym.pojo.Advertisement;

@Service
public class IndexService {

	// 从配置文件中读取后台服务器的地址
	@Value("${MANAGE_BASE_URL}")
	private String MANAGE_BASE_URL;
	// 从配置文件中读取广告1的资源路径
	@Value("${INDEXAD1_PATH}")
	private String INDEXAD1_PATH;
	// 从配置文件中读取右侧广告的资源路径
	@Value("${INDEXRAD_PATH}")
	private String INDEXRAD_PATH;
	
	// 从配置文件中读取轮播广告的资源路径
	@Value("${INDEXCROLLAD_PATH}")
	private String INDEXCROLLAD_PATH;

	//redis缓存中大广告的key
	@Value("${REDIS_INdDEX_AD1_KEY}")
	private String REDIS_INdDEX_AD1_KEY;
	
	//redis缓存中右侧小广告的key
	@Value("${REDIS_INdDEX_AD2_KEY}")
	private String REDIS_INdDEX_AD2_KEY;
	
	//redis缓存轮播广告的key
	@Value("${REDIS_INdDEX_AD3_KEY}")
	private String REDIS_INdDEX_AD3_KEY;
	
	
	//设置redis的存活时间(单位秒)
	@Value("${REDIS_ITEM_CAT_AKK_TIME}")
	private Integer REDIS_ITEM_CAT_AKK_TIME;
	
	@Autowired
	private ApiService apiService;

	@Autowired
	private RedisService redisService;
	
	private ObjectMapper mapper = new ObjectMapper();

	/**
	 * 前台系统查询首页大广告6个content对象， { "created": null, "updated": null, "id": 1,
	 * "categoryId": 13, "title": "大广告", "subTitle": "哈好", "titleDesc": "阿斯发动机",
	 * "url": "http://www.jd.com", "pic":
	 * "http://image.xym.com/images/2017/12/30/2017123009011077706149.jpg",
	 * "pic2":
	 * "http://image.xym.com/images/2017/12/30/201712300901279770232.jpg",
	 * "content":
	 * "<img src=\"http://manage.xym.com/js/kindeditor-4.1.10/plugins/emoticons/images/10.gif\" border=\"0\" alt=\"\" />"
	 * } 进行封装返回json数据 { "src":
	 * "http://image.xym.com/images/2017/12/28/2017122809162960407793.jpg",
	 * "width": 670, "height": 240, "alt": "", "href":
	 * "http://sale.jd.com/act/e0FMkuDhJz35CNt.html?cpdad=1DLSUE", "srcB":
	 * "http://image.xym.com/images/2017/12/28/2017122809162960407793.jpg",
	 * "widthB": 550, "heightB": 240 }
	 * 
	 * @throws Exception
	 */
	public String queryIndexAD1() throws Exception {
		
		
		String ADlJson = redisService.get(REDIS_INdDEX_AD1_KEY);
		
		if(ADlJson!=null){
			return ADlJson;
		}

		List<Advertisement> ADlist = new ArrayList<Advertisement>();

		// 大广告路径
		String url = MANAGE_BASE_URL + INDEXAD1_PATH;
		// 查询后台接口，获取json格式数据
		String contentAD1 = apiService.doGet(url);
		// 解析JSON数据
		@SuppressWarnings("unchecked")
		List<LinkedHashMap<String, Object>> list = mapper.readValue(contentAD1,
				List.class);
		for (int i = 0; i <list.size(); i++) {
			LinkedHashMap<String, Object> map = list.get(i);
			Advertisement indexAd1 = new Advertisement();
			indexAd1.setSrc(map.get("pic").toString());
			indexAd1.setAlt(map.get("title").toString());
			indexAd1.setHref(map.get("url").toString());
			indexAd1.setSrcB(map.get("pic2").toString());
			ADlist.add(indexAd1);
		}
		
		ADlJson = mapper.writeValueAsString(ADlist);
		redisService.set("REDIS_INdDEX_AD1_KEY", ADlJson,REDIS_ITEM_CAT_AKK_TIME );
		return ADlJson;

	}
	
	/**
	 * 右侧广告
	 * "width": 310,
        "height": 70,
        "src": "/images/5440ce68Na00d019e.jpg",
        "href": "http://c.fa.jd.com/adclick?sid=2&cid=601&aid=3614&bid=4196&unit=35984&advid=109277&guv=&url=http://sale.jd.com/mall/FQLUNlG53wbX7m.html",
        "alt": "",
        "widthB": 210,
        "heightB": 70,
        "srcB": "http://img14.360buyimg.com/da/jfs/t334/155/1756719493/14371/e367c503/5440ce6dNd056ce39.jpg"
	 * @return
	 * @throws Exception
	 */
	public String queryIndexRAD() throws Exception {
		
		String RightAD = redisService.get(REDIS_INdDEX_AD2_KEY);
		if(RightAD!=null){
			return RightAD;
		}

		List<Advertisement> ADlist = new ArrayList<Advertisement>();

		// 广告路径
		String url = MANAGE_BASE_URL + INDEXRAD_PATH;
		// 查询后台接口，获取json格式数据
		String contentAD1 = apiService.doGet(url);
		// 解析JSON数据
		@SuppressWarnings("unchecked")
		List<LinkedHashMap<String, Object>> list = mapper.readValue(contentAD1,
				List.class);
		
		for (LinkedHashMap<String, Object> map : list) {
			Advertisement indexAd1 = new Advertisement();
			indexAd1.setSrc(map.get("pic").toString());
			indexAd1.setAlt(map.get("title").toString());
			indexAd1.setHref(map.get("url").toString());
			indexAd1.setSrcB(map.get("pic2").toString());
			indexAd1.setHeight(70);
			indexAd1.setWidth(310);
			ADlist.add(indexAd1);
		}
		String asString = mapper.writeValueAsString(ADlist);
		
		redisService.set(REDIS_INdDEX_AD2_KEY, asString, REDIS_ITEM_CAT_AKK_TIME);
		
		return asString;

	}

	/**
	 * 查询轮播广告
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public String queryIndexCrollAD() throws JsonParseException, JsonMappingException, IOException {
		String RightAD = redisService.get(REDIS_INdDEX_AD3_KEY);
		if(RightAD!=null){
			return RightAD;
		}

		List<Advertisement> ADlist = new ArrayList<Advertisement>();

		// 广告路径
		String url = MANAGE_BASE_URL + INDEXCROLLAD_PATH;
		// 查询后台接口，获取json格式数据
		String contentAD1 = apiService.doGet(url);
		// 解析JSON数据
		@SuppressWarnings("unchecked")
		List<LinkedHashMap<String, Object>> list = mapper.readValue(contentAD1,
				List.class);
		
		for (LinkedHashMap<String, Object> map : list) {
			Advertisement indexAd1 = new Advertisement();
			indexAd1.setSrc(map.get("pic").toString());
			indexAd1.setAlt(map.get("title").toString());
			indexAd1.setHref(map.get("url").toString());
			indexAd1.setSrcB(map.get("pic2").toString());
			indexAd1.setHeight(70);
			indexAd1.setWidth(310);
			ADlist.add(indexAd1);
		}
		
		String asString = mapper.writeValueAsString(ADlist);
		
		redisService.set(REDIS_INdDEX_AD3_KEY, asString, REDIS_ITEM_CAT_AKK_TIME);
		
		return asString;

	}
}
